import Vue from 'vue';
import Formatter from '../framework/formatter';
import _ from 'lodash';
import Util from '../framework/util';
import { isIterable } from 'core-js';
import config from "../config"

Vue.filter('date', (value, format) => {
    return Formatter.formatDate(value, format);
});
Vue.filter('datetime', (value, format) => {
    return Formatter.formatDateTime(value, format);
});
Vue.filter('daterange', (date1, date2, format, separator = "~") => {
    return Formatter.formatDate(date1, format) + separator + Formatter.formatDate(date2, format);
});
Vue.filter('week', (value) => {
    return Formatter.formatWeek(value);
});
Vue.filter('prefix', (value, prefix) => {
    return prefix + value;
});
Vue.filter('suffix', (value, suffix) => {
    return value + suffix;
});
Vue.filter('format', (value, format) => {
    return format.format(value);
});
Vue.filter('money', (value, pattern) => {
    if (value === '' || value === undefined || value === null) {
        return '';
    }
    return Formatter.formatMoney(value, pattern);
});
Vue.filter('moneyAuto', (value, pattern) => {
    return Formatter.formatMoneyAuto(value, pattern);
});
Vue.filter('number', (value, pattern) => {
    return Formatter.formatNumber(value, pattern);
});
Vue.filter('percent', (value, pattern) => {
    return Formatter.formatNumber(value * 100, pattern) + "%";
});
Vue.filter('enums', (value, options) => {
    if (value != undefined) {
        let re = _.findLast(options,
            r => {
                return _.toUpper(r.value) == _.toUpper(value);
            }
        );
        if (re != undefined) {
            return re.key || value || "";
        }
    }
    return value || "";
});
Vue.filter('DictItem', (value, options, keyName, vlaueName) => {
    if (value != undefined) {
        let target = _.find(options,
            item => {
                if (vlaueName) {
                    return _.toUpper(item[vlaueName]) == _.toUpper(value);
                } else {
                    return _.toUpper(item.code) == _.toUpper(value) || _.toUpper(item.itemId) == _.toUpper(value);
                }
            }
        );
        if (target != undefined) {
            if (keyName) {
                return target[keyName] || "";
            } else {
                return target.name || "";
            }
        }
    }
    return value || "";
});
Vue.filter('imageSrc', (value) => {

  return config.server+value;
});

/**
 * 拼接过滤器
 * 如：{{models.Provinces | join(',',t=>t.province)}}
 */
Vue.filter('join', (value, pattern, filter) => {
    let array = Util.buildArray(value);
    if (_.isFunction(filter)) {
        array = array.map(t => {
            return filter(t);
        })
    }
    return array.join(pattern);
});
Vue.filter('defaultValue', (value, defaultValue) => {
    if (value == undefined) {
        return defaultValue;
    }
    return value;
});
Vue.filter('fileSize', (value) => {
    return Formatter.formatFileSize(value);
});
Vue.filter('clearHtml', (value) => {
    return Util.htmlClear(value);
});

// v-html 不能结合过滤器使用
Vue.filter('clearStyle', (value) => {
    if (!value) {
        return '';
    }
    value = value.replace(/style=[^\s>]+/, '');
    value = value.replace(/align=[^\s>]+/, '');
    return value;
});

Vue.filter('mapStatic', (value) => {
    if (!value) {
        return value;
    }
    else if (window.plus) {
        return value.startsWith("/") ? value.substring(1) : value;
    } else {
        return value.startsWith("/") ? value : "/" + value;
    }
});
Vue.filter('ellipsis', (value, length) => {
    if (value) {
        if (value.length > length) {
            return value.substring(0, length) + "...";
        }
        return value;
    } else {
        return "";
    }
});

Vue.filter('toFixed', (value, length = 2) => {
    return parseFloat(value).toFixed(length);
});
// 是数字，则按照指定小数点位数四舍五入返回；不是数字返回formatNaN
Vue.filter('numberToFixed', (value, length = 2, formatNaN = '--') => {
    let reg = /^[-]{0,1}[0-9]+.?[0-9]*$/;
    if (reg.test(value)) {
        let retVal = parseFloat(value).toFixed(length);
        if (/^[-]{1}[0]{1}.?[0]*$/.test(retVal)) {
            return retVal.toString().substr(1);
        }
        return retVal;
    }
    return formatNaN;
});
Vue.filter('distance', (value) => {
    if (value == undefined) {
        return "未知";
    }
    value = value || 0;
    if (value >= (10 * 1000 * 1000)) {
        return floor((value / (10 * 1000 * 1000.0)).toFixed(2)) + "万公里";
    }
    else if (value >= (1000 * 1000)) {
        return floor((value / (1000 * 1000.0)).toFixed(2)) + "千公里";
    }
    else if (value >= 1000) {
        return floor((value / 1000.0).toFixed(2)) + "公里";
    }
    else {
        return floor(value.toFixed(2)) + "米";
    }

    function floor(v) {
        if (Math.floor(v) == v) {
            return Math.floor(v);
        }
        return v;
    }
});
Vue.filter('json', (value) => {
    if (_.isString(value)) {
        return JSON.parse(value);
    } else {
        return value;
    }

});
Vue.filter('replace', (value, oldValue, newValue) => {
    if (value != undefined) {
        return _.replace(value, oldValue, newValue);
    }
    return value;
});
Vue.filter('quarter', (value) => {
    let quarters = ["一季度", "二季度", "三季度", "四季度"];
    return quarters[Math.ceil(value / 3) - 1];
});
Vue.filter('has', (value, optionKey = 'key') => {
    if (value != undefined && value.length > 0) {
        return value.filter(item => {
            return this.$framework.account.hasPermission(item[optionKey]);
        });
    }
    return [];
});

export default {};
